const app = getApp()
Page({
  goLogin() {
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  data: {
    avatar: '',
    nickName: '',
  },
  onLoad() {
    // 1、如果用户登录了再请求获取用户信息的接口
    app.token && this.getUserInfo()
  },
  async getUserInfo() {
    const { code, data } = await wx.http.get('/userInfo')
    if (code !== 10000) return wx.utils.toast('获取用户信息失败')
    this.setData({
      avatar: data.avatar,
      nickName: data.nickName,
    })

    // app上存储一份
    app.userProfile = {
      avatar: data.avatar,
      nickName: data.nickName,
    }
  },
})
